/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBuscarPadrones.java
 *
 * Created on 04/04/2011, 01:34:53 AM
 */

package verins.GUI;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.table.DefaultTableModel;
import verins.BO.EstadoPadronBO;
import verins.BO.PadronElectoralBO;
import verins.Utils.Comunes;
import verins.Utils.Mensajes;
import verins.beans.Estadopadron;
import verins.beans.Padronelectoral;
import verins.beans.Partidopoliticoxprocesoelectoral;
import verins.beans.Procesoelectoral;
import verins.beans.Usuario;

/**
 *
 * @author Hector
 */
public class frmBuscarPadrones extends JDialog {
    private Procesoelectoral procesoElec=null;
    private Usuario usuario=null;
    public Padronelectoral padronBusq=null;
    private List<Padronelectoral> lstPadrones=null;
    /** Creates new form frmBuscarPadrones */
    public frmBuscarPadrones(JDialog vent, Usuario user,Procesoelectoral proceso) {
        super(vent,true);
        initComponents();
        lblCoincidencias.setVisible(false);
        Comunes.centrarVentana(this);
        getRootPane().setDefaultButton(btnBuscar);
        llenarCombos();
    }

    
    private void llenarCombos(){
        List lista=null;
        lista=EstadoPadronBO.findAllPadron();
        verins.Utils.Comunes.llenarCombo(cmbEstado, lista);
        cmbEstado.insertItemAt(new Estadopadron(0,"Todos"), 0);
        cmbEstado.setSelectedIndex(1);
        
        
    }
    
    public void llenarTabla(){
        //NumberFormat formatter = new DecimalFormat("00000");  
        verins.Utils.Comunes.limpiarTabla(tblPadrones);
        DefaultTableModel dtm=(DefaultTableModel)tblPadrones.getModel();
        if (lstPadrones!=null&& lstPadrones.size()>0){
            Iterator it=lstPadrones.iterator();
            Padronelectoral p=null;
            while(it.hasNext()){
                p=(Padronelectoral)it.next();
                Object fila[]=new Object[] {p.getIdpadronelectoral(), 
                                            p.getIdpartidopolitico().getNombre(),
                                            p.getEstado().getDescripcion()};
                dtm.addRow(fila);
            }
            Comunes.setearColumnas(tblPadrones);
        }
        
    }
    
    private void obtenerCriterios(){
        padronBusq=new Padronelectoral();
        padronBusq.setIdpadronelectoral(txtPadron.getText());
        padronBusq.setIdpartidopolitico(((Partidopoliticoxprocesoelectoral)cmbPartido.getSelectedItem()).getPartidopolitico());
        padronBusq.setEstado((Estadopadron)cmbEstado.getSelectedItem());
        padronBusq.setIdprocesoelectoral(procesoElec.getIdprocesoelectoral()!=null?procesoElec.getIdprocesoelectoral():0);
        
    }
    
    private void  actualizarComboPartidos(){
        cmbPartido.removeAllItems();
        List lst=procesoElec.getPartidopoliticoxprocesoelectoralList();
        Comunes.llenarCombo(cmbPartido, lst);
      
    }
    
    public void setearProcesoBusq(Procesoelectoral proceso){
        procesoElec=proceso;
        txtProceso.setText(procesoElec.getNombre());
        btnBusqProceso.setEnabled(false);
        actualizarComboPartidos();
    }
    
    public void ocultarbtnSelec(){
        btnSeleccionar.setVisible(false);
    }
    
    private boolean validarCampos(){
    
        if (txtProceso.getText().trim().equals("")){
            Mensajes.alerta("Debe seleccionar un proceso");
            return false;
        }
         if (cmbPartido.getItemCount()==0){
            Mensajes.alerta("No se tienen partidos registrados");
            return false;
        }
        return true;
    }
    
    private Padronelectoral obtenerPadronSeleccionado(){
       
         if (lstPadrones!=null){
            int fila=tblPadrones.getSelectedRow();
                if (fila>=0){
                    padronBusq=lstPadrones.get(fila);
                } else 
                    padronBusq= null;
         }
           return padronBusq;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProceso = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        cmbPartido = new javax.swing.JComboBox();
        txtPadron = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cmbEstado = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtProceso = new javax.swing.JTextField();
        btnBusqProceso = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPadrones = new javax.swing.JTable();
        btnSalir = new javax.swing.JButton();
        btnDetalle = new javax.swing.JButton();
        lblCoincidencias = new javax.swing.JLabel();
        btnSeleccionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Búsqueda de padrones");

        lblProceso.setText("ELECCIONES PRESIDENCIALES 2011");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de búsqueda"));

        jLabel2.setText("Partido");

        jLabel1.setText("Nro Padrón");

        jLabel5.setText("Estado");

        jLabel3.setText("Proceso electoral");

        txtProceso.setEnabled(false);

        btnBusqProceso.setText("...");
        btnBusqProceso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBusqProcesoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(61, 61, 61)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtProceso, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtPadron, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 203, Short.MAX_VALUE)
                                        .addComponent(jLabel5)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(btnBusqProceso, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(jLabel3))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtProceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBusqProceso))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtPadron, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cmbPartido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        tblPadrones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Padrón", "Partido", "Estado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblPadrones.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(tblPadrones);
        tblPadrones.getColumnModel().getColumn(0).setResizable(false);
        tblPadrones.getColumnModel().getColumn(1).setResizable(false);
        tblPadrones.getColumnModel().getColumn(2).setResizable(false);

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnDetalle.setText("Ver detalle");
        btnDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDetalleActionPerformed(evt);
            }
        });

        lblCoincidencias.setText("Se encontraron (0) coincidencias");

        btnSeleccionar.setText("Seleccionar");
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSeleccionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 206, Short.MAX_VALUE)
                        .addComponent(btnDetalle)
                        .addGap(215, 215, 215)
                        .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblCoincidencias)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 368, Short.MAX_VALUE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblProceso))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblProceso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(lblCoincidencias))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalir)
                    .addComponent(btnDetalle)
                    .addComponent(btnSeleccionar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDetalleActionPerformed
        // TODO add your handling code here:
        obtenerPadronSeleccionado();
        if (padronBusq!=null){
            if (!padronBusq.getPaginapadronelectoralList().isEmpty()){
                frmTratarPadron frm=new frmTratarPadron(usuario, this, padronBusq);
                frm.setVisible(true);
            }else{
                Mensajes.alerta("El padrón no tiene firmantes registrados");
            }
        } 
    }//GEN-LAST:event_btnDetalleActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        if (validarCampos()){
            obtenerCriterios();
            lstPadrones=PadronElectoralBO.findByCriterios(padronBusq);
            llenarTabla();
            if (lstPadrones.isEmpty())
                lblCoincidencias.setVisible(true);
            else
                lblCoincidencias.setVisible(false);
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBusqProcesoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBusqProcesoActionPerformed
        // TODO add your handling code here:
        frmBusquedaProcesoElectoral frm=new frmBusquedaProcesoElectoral(usuario, null);
        frm.setearBotonesBusqueda();
        frm.setVisible(true);
        procesoElec=frm.proceso;
        if (procesoElec!=null){
            procesoElec=frm.proceso;
            txtProceso.setText(procesoElec.getNombre());
            actualizarComboPartidos();
        }
    }//GEN-LAST:event_btnBusqProcesoActionPerformed

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        // TODO add your handling code here:
        obtenerPadronSeleccionado();
        this.dispose();
    }//GEN-LAST:event_btnSeleccionarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmBuscarPadrones(null,null,null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBusqProceso;
    private javax.swing.JButton btnDetalle;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbPartido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCoincidencias;
    private javax.swing.JLabel lblProceso;
    private javax.swing.JTable tblPadrones;
    private javax.swing.JTextField txtPadron;
    private javax.swing.JTextField txtProceso;
    // End of variables declaration//GEN-END:variables

}
